package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.message.*;
import sk.naive.talker.persistence.*;

import java.rmi.RemoteException;

/**
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.24 $ $Date: 2005/01/19 22:41:27 $
 */
public class Write extends AbstractCommand {
	public void exec() throws CommandException, RemoteException {
		BoardHelper bh = new BoardHelper(commandDispatcher, user, params);
		Board board = bh.getBoard();
		if (board == null) {
			user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL, params);
			sendHelper().sendMessage(user, bh.getWarningMessageKey());
			return;
		}
		user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL, board.getName());
		new EditorHelper(this, board).start(board);
	}

	private class EditorHelper extends AbstractEditorHelper {
		private Board board;

		public EditorHelper(AbstractCommand comm, Board board) {
			super(Write.this.user, comm);
			this.board = board;
		}

		protected Text getOriginalText(PersistentObject object, TextPersistence db) throws PersistenceException {
			return null;
		}

		protected int textType() {
			return TextRelation.BOARD_POST;
		}

		protected void sendNotify() throws RemoteException {
			ctxSet(DefaultMessageFactory.CTXKEY_VAL, board.getName());
			super.sendNotify();
		}

		protected String getUserKeyBegin() {
			return "write.begin";
		}

		protected String getUserKeySend() {
			return "write.successful";
		}

		protected String getAudienceKeySend() {
			return "write.audienceOK";
		}

		protected Category getAudienceCategorySend() {
			return Category.BOARD;
		}

		protected Text createNewText(TextPersistence tp, String s) throws PersistenceException {
			Text t = new Text(tp, s, user.getId());
			board.add(t);
			return t;
		}
	}
}
